<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\GoodsRequest;
use App\Services\GoodsService;
use App\Models\Goods;

class GoodsController extends Controller
{
    public function index(Request $request)
    {
    	$datas = Goods::filter($request->query())->with(['category','brand'])->latest()->paginate();
    	return view('dashboard.goods.index', ['datas' => $datas]);
    }

    public function create()
    {
    	return view('dashboard.goods.create');
    }

    public function store(GoodsRequest $request, GoodsService $service)
    {    
    	$service->saveGoods($request->all());
        return redirect()->route('admin.goods.index');
    }

    public function edit(Goods $good)
    {
        return view('dashboard.goods.create', ['data' => $good]);
    }

    public function update(GoodsRequest $request, GoodsService $service, $id)
    {
        $service->saveGoods($request->all(), $id);
        return redirect()->route('admin.goods.index');
    }

    public function destroy(Goods $good) {
        $good->delete();
        return response()->json(['status' => 0, 'msg' => '删除成功']);
    }
}
